package weaver.jobs.hggd.duss.cron;

import com.alibaba.fastjson.JSONObject;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.jobs.hggd.duss.utils.HggdUtil;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Title: ecology
 * @Company: weaver
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2024-07-22
 * @Description: 收文送签流程生成归档信息包
 * @ModifyLog:
 **/

public class WeaSwlc4CheckCronJob extends BaseCronJob {
    private static final String TABLENAME = "uf_swsxjc";
    private static final Integer FORMID = 50;
    private static final Integer SUCCESS = 0;
    private static final Integer FAILURE = 1;

    @Override
    public void execute() {
        RecordSet rs = new RecordSet();
        HggdUtil hggdUtil = new HggdUtil();
        BaseBean baseBean = new BaseBean();
        int czzt = FAILURE;
        String message = "";
        int seccategory = hggdUtil.getDoccategory(FORMID);
        String url = baseBean.getPropValue("arc4check", "url");
        String currentDate = DateUtil.getCurrentDate();
        baseBean.writeLog(">>>>>WeaSwlc4CheckCronJob>>>>>currentDate=" + currentDate);
        rs.executeQuery("select * from " + TABLENAME + " where gdrq=? and (czzt=1 or czzt is null)", currentDate);
        while (rs.next()) {
            Map<String, Object> fileDataMap = new HashMap<>();
            String requestid = Util.null2String(rs.getString("lcid"));//新增流程ID
            baseBean.writeLog(">>>>>WeaSwlc4CheckCronJob>>>>>requestid=" + requestid);
            String requestname = Util.null2String(rs.getString("lcmc"));//新增流程名称
            fileDataMap.put("swnf", Util.null2String(rs.getString("swnf"), " "));
            fileDataMap.put("bgqxwb", Util.null2String(rs.getString("bgqxwb")," "));
            String zwwj = Util.null2String(rs.getString("zwwj"));
            String fj = Util.null2String(rs.getString("fj"));
            String bdjqzyjfj = hggdUtil.getBdToPdfFile(FORMID, requestid);
            fileDataMap.put("djrwb", Util.null2String(rs.getString("djrwb")));
            fileDataMap.put("djrq", Util.null2String(rs.getString("djrq")));
            fileDataMap.put("lwdw", Util.null2String(rs.getString("lwdw")));
            fileDataMap.put("lwwh", Util.null2String(rs.getString("lwwh")));
            fileDataMap.put("wjlx", "收文");
            fileDataMap.put("lwxcrq", Util.null2String(rs.getString("lwxcrq")));
            fileDataMap.put("hjcdwb", Util.null2String(rs.getString("hjcdwb")," "));
            fileDataMap.put("cbrq", Util.null2String(rs.getString("cbrq")));
            fileDataMap.put("wjyszs", Util.null2String(rs.getString("wjyszs")));
            fileDataMap.put("wjfszs", Util.null2String(rs.getString("wjfszs")));
            fileDataMap.put("fjfszs", Util.null2String(rs.getString("fjfszs")));
            fileDataMap.put("zwwjwb", Util.null2String(rs.getString("zwwjwb"), " "));
            fileDataMap.put("fjwb", Util.null2String(rs.getString("fjwb"), " "));
            fileDataMap.put("flmc", Util.null2String(rs.getString("flmc"), " "));
            fileDataMap.put("flmcwb", Util.null2String(rs.getString("flmcwb"), " "));
            fileDataMap.put("djbmwb", Util.null2String(rs.getString("djbmwb"), " "));
            fileDataMap.put("fszs", Util.null2String(rs.getString("fszs")));
            fileDataMap.put("bz", Util.null2String(rs.getString("bz"), " "));
            fileDataMap.put("mjwb", Util.null2String(rs.getString("mjwb"), " "));
            String target = "/";
            String baseDirPath = baseBean.getPropValue("arc4check", "basedirpath");
            String appid = "oa";
            try {
                String md5 = hggdUtil.generateMD5(requestid);
                String tarDirName = appid + "-" + hggdUtil.getDateDir("") + "-" + md5;
                String fullTarDirPath = baseDirPath + appid + target + hggdUtil.getDateDir(target) + target + tarDirName;
                String fullTarDirName = fullTarDirPath + ".tar";
                String timestamp = hggdUtil.getTimestamp();
                String filepath = "";
                List<Map<String, Object>> imagefileList = new ArrayList<>();
                //获取正文文件信息
                if (!zwwj.isEmpty()) {
                    String[] docidList = zwwj.split(",");
                    for (String docid : docidList) {
                        Map<String, Object> docidMap = hggdUtil.getImagefileInfo(docid);
                        String imagefileid = Util.null2String(docidMap.get("imagefileid"));
                        String operatedate = Util.null2String(docidMap.get("operatedate"));
                        String operatetime = Util.null2String(docidMap.get("operatetime"));
                        String imagefilename = Util.null2String(docidMap.get("imagefilename"));
                        String imagefiletype = Util.null2String(docidMap.get("imagefiletype"));
                        String filesize = Util.null2String(docidMap.get("filesize"));
                        InputStream inputStream = hggdUtil.getInputStreamById(imagefileid);
                        hggdUtil.createFile(inputStream, fullTarDirPath + target + timestamp, imagefilename);
                        String filetype = "";
                        String filename = "";
                        if (imagefilename.lastIndexOf(".") > -1) {
                            filetype = imagefilename.substring(imagefilename.lastIndexOf(".") + 1);
                            filename = imagefilename.substring(0, imagefilename.lastIndexOf("."));
                        }
                        Map<String, Object> imagefileMap = new HashMap<>();
                        imagefileMap.put("docid", docid);
                        imagefileMap.put("imagefileid", imagefileid);
                        imagefileMap.put("imagefiletype", imagefiletype);
                        imagefileMap.put("filetype", filetype);
                        imagefileMap.put("filename", filename);
                        imagefileMap.put("imagefilename", imagefilename);
                        imagefileMap.put("filesize", filesize);
                        imagefileMap.put("doccreatedate", operatedate);
                        imagefileMap.put("doccreatetime", operatetime);
                        baseBean.writeLog(">>>>>WeaSwlc4CheckCronJob>>>>>imagefileMap=" + imagefileMap);
                        imagefileList.add(imagefileMap);
                        filepath += "".equals(filepath) ? timestamp + target + imagefilename : "," + timestamp + target + imagefilename;
                    }
                }
                //获取附件文件信息
                if (!fj.isEmpty()) {
                    String[] jfList = fj.split(",");
                    for (String docid : jfList) {
                        Map<String, Object> docidMap = hggdUtil.getImagefileInfo(docid);
                        String imagefileid = Util.null2String(docidMap.get("imagefileid"));
                        String operatedate = Util.null2String(docidMap.get("operatedate"));
                        String operatetime = Util.null2String(docidMap.get("operatetime"));
                        String imagefilename = Util.null2String(docidMap.get("imagefilename"));
                        String imagefiletype = Util.null2String(docidMap.get("imagefiletype"));
                        String filesize = Util.null2String(docidMap.get("filesize"));
                        InputStream inputStream = hggdUtil.getInputStreamById(imagefileid);
                        hggdUtil.createFile(inputStream, fullTarDirPath + target + timestamp, imagefilename);
                        String filetype = "";
                        String filename = "";
                        if (imagefilename.lastIndexOf(".") > -1) {
                            filetype = imagefilename.substring(imagefilename.lastIndexOf(".") + 1);
                            filename = imagefilename.substring(0, imagefilename.lastIndexOf("."));
                        }
                        Map<String, Object> imagefileMap = new HashMap<>();
                        imagefileMap.put("docid", docid);
                        imagefileMap.put("imagefileid", imagefileid);
                        imagefileMap.put("imagefiletype", imagefiletype);
                        imagefileMap.put("filetype", filetype);
                        imagefileMap.put("filename", filename);
                        imagefileMap.put("imagefilename", imagefilename);
                        imagefileMap.put("filesize", filesize);
                        imagefileMap.put("doccreatedate", operatedate);
                        imagefileMap.put("doccreatetime", operatetime);
                        baseBean.writeLog(">>>>>WeaSwlc4CheckCronJob>>>>>imagefileMap=" + imagefileMap);
                        imagefileList.add(imagefileMap);
                        filepath += "".equals(filepath) ? timestamp + target + imagefilename : "," + timestamp + target + imagefilename;
                    }
                }
                //获取表单签字意见转pdf信息
                if (!bdjqzyjfj.isEmpty()) {
                    String[] bdjqzyjfjList = bdjqzyjfj.split(",");
                    for (String docid : bdjqzyjfjList) {
                        Map<String, Object> docidMap = hggdUtil.getImagefileInfo(docid);
                        String imagefileid = Util.null2String(docidMap.get("imagefileid"));
                        String operatedate = Util.null2String(docidMap.get("operatedate"));
                        String operatetime = Util.null2String(docidMap.get("operatetime"));
                        String imagefilename = Util.null2String(docidMap.get("imagefilename"));
                        String imagefiletype = Util.null2String(docidMap.get("imagefiletype"));
                        String filesize = Util.null2String(docidMap.get("filesize"));
                        InputStream inputStream = hggdUtil.getInputStreamById(imagefileid);
                        hggdUtil.createFile(inputStream, fullTarDirPath + target + timestamp, imagefilename);
                        String filetype = "";
                        String filename = "";
                        if (imagefilename.lastIndexOf(".") > -1) {
                            filetype = imagefilename.substring(imagefilename.lastIndexOf(".") + 1);
                            filename = imagefilename.substring(0, imagefilename.lastIndexOf("."));
                        }
                        Map<String, Object> imagefileMap = new HashMap<>();
                        imagefileMap.put("docid", docid);
                        imagefileMap.put("imagefileid", imagefileid);
                        imagefileMap.put("imagefiletype", imagefiletype);
                        imagefileMap.put("filetype", filetype);
                        imagefileMap.put("filename", filename);
                        imagefileMap.put("imagefilename", imagefilename);
                        imagefileMap.put("filesize", filesize);
                        imagefileMap.put("doccreatedate", operatedate);
                        imagefileMap.put("doccreatetime", operatetime);
                        baseBean.writeLog(">>>>>WeaSwlc4CheckCronJob>>>>>imagefileMap=" + imagefileMap);
                        imagefileList.add(imagefileMap);
                        filepath += "".equals(filepath) ? timestamp + target + imagefilename : "," + timestamp + target + imagefilename;
                    }
                }
                fileDataMap.put("requestid", hggdUtil.generateMD5(requestid));
                fileDataMap.put("requestname", requestname);
                fileDataMap.put("imagefileList", imagefileList);
                fileDataMap.put("destFilePath", fullTarDirPath + target + timestamp + target + timestamp + ".xml");
                baseBean.writeLog(">>>>>WeaSwlc4CheckCronJob>>>>>fileDataMap=" + fileDataMap);
                if (!bdjqzyjfj.isEmpty() && !zwwj.isEmpty()) {
                    //生成xml文件
                    boolean fileDataStatus = hggdUtil.fileDataToXml(fileDataMap);
                    Map<String, Object> archiveMap = new HashMap<>();
                    archiveMap.put("dataXmlPath", timestamp + target + timestamp + ".xml");
                    archiveMap.put("filePath", filepath);
                    archiveMap.put("destFilePath", fullTarDirPath + target + "档案包目录.xml");
                    baseBean.writeLog(">>>>>WeaSwlc4CheckCronJob>>>>>archiveMap=" + archiveMap);
                    //生成目录xml文件
                    boolean archiveDirStatus = hggdUtil.archiveDirectoryToXml(archiveMap);
                    boolean createTarStatus = hggdUtil.createTarFromDirectory(fullTarDirPath, fullTarDirName);
                    baseBean.writeLog(">>>>>WeaSwlc4CheckCronJob>>>>>fileDataStatus=" + fileDataStatus);
                    baseBean.writeLog(">>>>>WeaSwlc4CheckCronJob>>>>>archiveDirStatus=" + archiveDirStatus);
                    baseBean.writeLog(">>>>>WeaSwlc4CheckCronJob>>>>>createTarStatus=" + createTarStatus);
                    if (fileDataStatus && archiveDirStatus && createTarStatus) {
                        int docids = hggdUtil.createDocid(fullTarDirName, fullTarDirName, seccategory);
                        if (docids <= 0) {
                            message = "生成文档失败";
                        } else {
                            rs.executeUpdate("update " + TABLENAME + " set gdxxb=? where lcid=?", docids, requestid);
                            JSONObject paramJson = new JSONObject();
                            paramJson.put("reqid", requestid);
                            paramJson.put("appid", "OA");
                            paramJson.put("apptoken", "");
                            paramJson.put("companycode", "");
                            paramJson.put("fondsname", "");
                            Map<String, Object> docidMap = hggdUtil.getImagefileInfo(docids + "");
                            String imagefileid = Util.null2String(docidMap.get("imagefileid"));
                            String ddcode = hggdUtil.getTokenKey(imagefileid);
                            String tarfilePath = baseBean.getPropValue("arc4check", "oaurl") + "?fileid=" + imagefileid + "&download=1&ddcode=" + ddcode;
                            paramJson.put("tarfilefetch", tarfilePath);
                            paramJson.put("aiptitle", tarDirName + ".tar");
                            paramJson.put("fileid", timestamp);
                            paramJson.put("model", "f");
                            paramJson.put("aipdigest", hggdUtil.generateMD5(requestid));
                            JSONObject resultJson = hggdUtil.sendOKHttpRequest(paramJson, url);
                            baseBean.writeLog(">>>>>WeaSwlc4CheckCronJob>>>>>resultJson=" + resultJson);
                            JSONObject dataJson = resultJson.getJSONObject("data");
                            String code = Util.null2String(dataJson.getString("code"));
                            if (!"200".equals(code)) {
                                String desc = Util.null2String(dataJson.getString("desc"));
                                message = "档案系统接口返回：" + desc;
                            } else {
                                czzt = SUCCESS;
                                message = "档案系统接口接收成功";
                            }
                        }
                    } else {
                        message = "创建档案归档信息包失败";
                    }
                } else {
                    message = "表单转PDF文件未生成或正文未上传";
                }
            } catch (Exception e) {
                message = "接口处理异常,查看日志搜索关键字>>>>>WeaSwlc4CheckCronJob>>>>>Exception=";
                baseBean.writeLog(">>>>>WeaSwlc4CheckCronJob>>>>>Exception=" + e);
            }
            rs.executeUpdate("update " + TABLENAME + " set czzt=?,msg=? where lcid=?", czzt, message, requestid);
        }
    }
}
